package com.nit.lab.controller.basic;

import com.alibaba.fastjson.JSONObject;
import com.nit.lab.dto.basic.DeptDTO;
import com.nit.lab.dto.system.TokenDTO;
import com.nit.lab.dto.utils.OpResultDTO;
import com.nit.lab.dto.utils.TableReqDTO;
import com.nit.lab.dto.utils.TableRspDTO;
import com.nit.lab.service.basic.DeptService;
import com.nit.lab.utils.HttpRequestReader;
import com.nit.lab.utils.JWTUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @Description
 * @Author HWY
 * @Date 2020/12/14 10:46
 **/
@RestController
@RequestMapping(value = "/api/dept")
public class DeptController {

    @Resource
    private DeptService deptService;
    private final Logger logger = LoggerFactory.getLogger(DeptController.class);

    /**
     * @Description: 获取数据列表
     * @Author HWY
     * @Date 2020/12/14
     */
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public TableRspDTO list4Table(@RequestBody TableReqDTO tableReqDTO) {
        TableRspDTO tableRspDTO = new TableRspDTO();
        try {
            tableRspDTO = deptService.list4Table(tableReqDTO);
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return tableRspDTO;
    }

    /**
     * @Description: 新增记录
     * @Author HWY
     * @Date 2020/12/14
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public OpResultDTO add(@RequestBody DeptDTO deptDTO) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            opResult.setMessage(deptService.add(deptDTO).toString());
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }

    /**
     * @Description: 更新记录
     * @Author HWY
     * @Date 2020/12/14
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public OpResultDTO update(@RequestBody DeptDTO unitDTO) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            opResult.setMessage(deptService.update(unitDTO).toString());
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }

    /**
     * @Description: 删除记录
     * @Author HWY
     * @Date 2020/12/14
     */
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    public OpResultDTO remove(HttpServletRequest request) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            JSONObject jsonRequest = HttpRequestReader.getJsonObject(request);
            String deptIds = jsonRequest.getString("deptIds");
            opResult.setMessage(deptService.remove(deptIds).toString());
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }

    /**
     * @Description: 获取单位信息用于选择
     * @Author: HWY
     * @Date: 2020/12/25
     */
    @RequestMapping(value = "/listDept", method = RequestMethod.POST)
    public OpResultDTO listDept() {
        OpResultDTO opResult = new OpResultDTO();
        try {
            opResult.setMessage("success");
            opResult.setResult(deptService.listDept());
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }

    /**
     * @Description: 获取单位信息树
     * @Author HWY
     * @Date 2020/12/20
     */
    @RequestMapping(value = "/listDeptTree", method = RequestMethod.POST)
    public OpResultDTO listDeptTree(@RequestHeader(name = "Access-Token") String token) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            TokenDTO tokenDTO = JWTUtil.verifyToken(token);
            opResult.setMessage("success");
            opResult.setResult(deptService.listDeptTree(tokenDTO.getDeptId(), tokenDTO.getDeptType()));
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }
}
